<?php
// @formatter:off
/**
 * @file WCProfile.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-17 01:26:11 +0000 (Mon, 17 Jun 2013) $
 *
 * $Id: WCProfile.php 74 2013-06-17 01:26:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/includes/security/WCProfile.php $
 */
// @formatter:on

/**
 * @class WCProfile
 */
class WCProfile {
	protected $_cacheCode = false;
	protected $_code = false;
	protected $_name = false;
	protected $_grants = array();
	protected $_grantedGroups = false;
	protected $_grantedPublic = false;
	protected $_user = null;

	public function __construct() {
	}
	//
	// Public Methods.
	/**
	 * Checks a list of permission codes for any grant.
	 *
	 * @return boolean Returns true when any of the asked permission is
	 * granted. When no permission is asked, returns false.
	 */
	public function allowedAny() {
		$allowed = false;

		foreach(func_get_args() as $perm) {
			if($this->granted($perm)) {
				$allowed = true;
				break;
			}
		}

		return $allowed;
	}
	/**
	 * Checks a list of permission codes.
	 *
	 * @return boolean Returns true when all asked permission are granted.
	 * When no permission is asked, returns false.
	 */
	public function allowedTo() {
		$allowed = false;
		$first = true;

		foreach(func_get_args() as $perm) {
			$allowed = ($allowed || $first) && $this->granted($perm);
			$first = false;
		}

		return $allowed;
	}
	public function cacheCode() {
		if($this->_cacheCode === false) {
			$this->_cacheCode = "{$this->code()}|";
			$this->_cacheCode .= $this->grantedPublic() ? "1|" : "0|";
			$this->_cacheCode .= implode("|", $this->_grantedGroups);

			$this->_cacheCode = md5($this->_cacheCode);
		}

		return $this->_cacheCode;
	}
	public function code() {
		return $this->_code;
	}
	public function granted($perm) {
		return $this->code() == "ROOT" || in_array($perm, $this->_grants);
	}
	public function grantedGroup(WCGroup &$group) {
		return $this->code() == "ROOT" || $group->isPublic() || in_array($group->id, $this->_grantedGroups);
	}
	public function load(WCUser &$user = null) {
		global $wcPermissions;

		if($user == null) {
			$this->_user = null;
			$this->_code = $wcPermissions["default"];
		} else {
			$this->_user = $user;
			$this->_code = $user->profileCode();

			$roots = array(
				WC_USER_ROOT_ID,
				WC_USER_CRON_ID
			);
			if($this->code() == "ROOT" && !in_array($user->id, $roots)) {
				$this->_code = isset($wcPermissions["pyramid"]["ROOT"]) ? $wcPermissions["pyramid"]["ROOT"] : "*";
			} elseif(in_array($user->id, $roots)) {
				$this->_code = "ROOT";
			}
		}

		$this->loadGroups();

		if($this->code() != "ROOT") {
			$this->loadPermissions($this->code());
		}
	}
	public function name() {
		return $this->_code;
	}
	//
	// Protected methods.
	protected function loadGroups() {
		if($this->_grantedGroups === false) {
			$this->_grantedGroups = array();

			if($this->_user && $this->_user->ok()) {
				global $wcGroupsHolder;

				foreach($wcGroupsHolder->groups() as $group) {
					if($this->code() == "ROOT" || $group->isPublic() || $this->_user->profileCode($group) != 'ANON') {
						$this->_grantedGroups[] = $group->id;
					}
				}
			}
		}
	}
	protected function loadPermissions($code) {
		global $wcPermissions;

		$this->loadPyramidPermissions($code);
		$this->_grants = array_unique(array_merge($this->_grants, $wcPermissions["direct-perms"][$code]));
	}
	protected function loadPyramidPermissions($code) {
		global $wcPermissions;

		$this->_grants = array_unique(array_merge($this->_grants, $wcPermissions["pyramid-perms"][$code]));

		if(isset($wcPermissions["pyramid"][$code])) {
			$this->loadPermissions($wcPermissions["pyramid"][$code]);
		}
	}
}
?>